//
//  AnimationViewController.m
//  RL_项目二
//
//  Created by rimi on 16/3/29.
//  Copyright © 2016年 ruifengpeixun. All rights reserved.
//

#import "AnimationViewController.h"
#import "WeatherDataRequest.h"
#import "FitOfScreen.h"
#import "YXScrollLabel.h"
#import "LotCloud.h"    //多云
#import "Snow.h"        //雪
#import "Cloud.h"       //下雨
#import "SunView.h"     //晴
#import "RainSnow.h"    //雨夹雪
#import "Fog.h"         //雾

@interface AnimationViewController ()
//分别控制滚动标签中两个数的值
@property (nonatomic , assign) BOOL *isLoad;
//计数的，通过求余 来形成分别从上和下滚动出来的效果
@property (nonatomic , assign) NSInteger upOrDown;
//字符串 存放温度
@property (nonatomic, strong) NSString *temperature;
//网络请求
@property (nonatomic, strong) WeatherDataRequest *weatherRequest;
//两个滚动视图
@property (nonatomic, strong) YXScrollLabel *laber1;
@property (nonatomic, strong) YXScrollLabel *laber2;
@property (nonatomic, strong) LotCloud *lotCloud;
@property (nonatomic, strong) Snow *snow;
@property (nonatomic, strong) Cloud *cloud;
@property (nonatomic, strong) SunView *sunView;
@property (nonatomic, strong) RainSnow *rainSnow;
@property (nonatomic, strong) Fog *fog;
//字典存取天气
@property (nonatomic, strong) NSDictionary *diction;

@end


@implementation AnimationViewController
- (instancetype)init:(NSString *)city
{
    self = [super init];
    if (self) {
        _weatherRequest = [[WeatherDataRequest alloc]init];
        [self httpRequst:city];
        }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];

     [self addObserver:self forKeyPath:@"weather" options:NSKeyValueObservingOptionNew context:nil];
   
 
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    NSLog(@"字典有信息了");
    [self addLabel];
    
    
}
- (NSArray < NSString * > *)dataOfScrollValue:(YXScrollLabel *)scrollLabel{
    NSInteger temperature = [_weatherRequest.weather[@"temperature"] integerValue];
    NSInteger tenDigits = temperature/10;       //十位数
    NSInteger SingleDigits = temperature%10;      //个位数
    NSArray *arrat = [NSArray array];
    if (scrollLabel == _laber1) {
        arrat  = @[@"9",@"8",@"1",@"3",@"5",@"4",@"8",@"1",@"3",@"5",@"7",@"8",[NSString stringWithFormat:@"%ld",tenDigits]];
    }else{
        arrat  = @[[NSString stringWithFormat:@"%ld",SingleDigits],@"1",@"3",@"5",@"4",@"8",@"1",@"3",@"5",@"7",@"4",@"8",@"8"];
    }
    return  arrat;
}//设置动画啥的？
- (YXScrollAnimationTimeOption)timeFunctionOfScrollAnimation:(YXScrollLabel *)scrollLabel{
    return CurveEaseOut;
}
//设置字体大小
- (UIFont *)fontForText:(YXScrollLabel *)scrollLabel{
    return [UIFont systemFontOfSize:95 * LRLRadioWidth];
}
//设置滚动的方向
- (YXScrollDirection)directionForScrollLabel:(YXScrollLabel *)scrollLabel{
    if (scrollLabel == _laber1) {
        return Top;
    }else {
        return Bottom;
    }
}
//如果请求到数据就执行加载图标
- (void)addLabel{
   
    for (id obj in self.view.subviews) {
        if ([obj isKindOfClass:[YXScrollLabel class]]) { [(YXScrollLabel *)obj removeFromSuperview]; }
        if ([obj isKindOfClass:[LotCloud class]]) {
            [_lotCloud disMiss];
        }else if ([obj isKindOfClass:[SunView class]]){
            [_sunView disMiss];
        }else if ([obj isKindOfClass:[Snow class]]){
            [_snow disMiss];
        }else if ([obj isKindOfClass:[RainSnow class]]){
            [_rainSnow disMiss];
        }else if ([obj isKindOfClass:[Fog class]]){
            [_fog disMiss];
        }else if ([obj isKindOfClass:[Cloud class]]){
            [_cloud disMiss];
        }
        if ([obj isKindOfClass:[UIImageView class]]) {
            [(UIImageView *)obj removeFromSuperview];
        }

    }

    //两个滚动标签的初始化
    _laber1  = [[YXScrollLabel alloc]initWithFrame:LRLCGRectMake( 25, 45, 90, 90) delegate:self];
    [self.view addSubview:_laber1];
    _laber2  = [[YXScrollLabel alloc]initWithFrame:LRLCGRectMake( 75, 45, 90, 90) delegate:self];
    [self.view addSubview:_laber2];
    //滚动视图后面的度的符号
    UIImageView *imageO = [[UIImageView alloc]initWithFrame:CGRectMake(120, 45, 1, 1)];
    imageO.image = [UIImage imageNamed:@"摄氏度符号"];
    [UIView animateWithDuration:1 animations:^{
        imageO.frame = LRLCGRectMake(135, 38, 35, 35);
    }];
    [self.view addSubview:imageO];
    
     NSString *weatherInfo =  _weatherRequest.weather[@"info"];
    if ([weatherInfo isEqualToString:@"多云"]) {
     
        _lotCloud = [[LotCloud alloc]initWithFrame:LRLCGRectMake(180, 45, 90, 90)];
        
        [self.view addSubview:_lotCloud];
    }else if ([weatherInfo isEqualToString:@"晴"]){
      
        _sunView = [[SunView alloc]initWithFrame:LRLCGRectMake(180, 45, 90, 90)];
        [self.view addSubview:_sunView];
    }
    else if ([weatherInfo isEqualToString:@"雪"]){
  
        _snow = [[Snow alloc]initWithFrame:LRLCGRectMake(180, 45, 90, 90)];
        [self.view addSubview:_snow];
    }
    else if ([weatherInfo isEqualToString:@"雨夹雪"]){
    
        _rainSnow = [[RainSnow alloc]initWithFrame:LRLCGRectMake(180, 45, 90, 90)];
        [self.view addSubview:_rainSnow];
    }
    else if ([weatherInfo isEqualToString:@"雾"]){
    
        _fog = [[Fog alloc]initWithFrame:LRLCGRectMake(180, 45, 90, 90)];
        [self.view addSubview:_fog];
    }else if ([weatherInfo isEqualToString:@"阴"]){
     
        _cloud = [[Cloud alloc]initWithFrame:LRLCGRectMake(180, 45, 90, 90)];
        [self.view addSubview:_cloud];
    }
    else{
    
        _lotCloud = [[LotCloud alloc]initWithFrame:LRLCGRectMake(180, 45, 95, 95)];
        [self.view addSubview:_lotCloud]; //未获得正确天气就显示多云
    }
}

- (void)httpRequst:(NSString *)city{

    if ([_weatherRequest.error isEqualToString:@"1"]) {

    }else{
        NSLog(@" 城市 %@",city);
    [_weatherRequest getData:^{
        NSLog(@"温度 viewcontroller %@",_weatherRequest.weather);
        NSLog(@"wwwwwwww  www%@",_weatherRequest.weather[@"info"]);
    } withcity:city];
    [_weatherRequest addObserver:self forKeyPath:@"weather" options:NSKeyValueObservingOptionNew context:nil];

}
}

@end
